package com.genITeam.ria.dao;

import java.util.*;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.hibernate.Session;

import org.hibernate.criterion.Expression;

import com.genITeam.ria.exception.NGFException;

import com.genITeam.ria.vo.Posts;

public class ViewForumDAO {
	public ViewForumDAO() {
		BasicConfigurator.configure();
	}

	Logger logger = Logger.getLogger(ViewForumDAO.class);

	/**
	 * getRootNode to to get the Root node of the tree
	 * 
	 * 
	 * @param Session
	 * @return Posts
	 * @throws NGFException
	 */

	public Posts getRootNode(Session session) throws NGFException {
		// System.out.println("in DAO - TreeDBConnection -> getRootNode()");
		Posts threadPostVo = new Posts();
		try {
			logger.info("start getRootNode");
			threadPostVo = (Posts) session.createCriteria(Posts.class).add(
					Expression.eq("title", "View Forum")).uniqueResult(); // .add(Expression.eq("postType",new
																			// Character('F')));

			// threadPostVo = (Posts)
			// session.createCriteria(Posts.class).add(Expression.eq("parentId",
			// new Integer(1000000))).uniqueResult() ;

			// ArrayList buddy = new ArrayList ();
			// buddy.addAll(buddyList);
			//			
			// String SQL_QUERY = "Select forumVO.postId, forumVO.parentId,
			// forumVO.title, forumVO.isLeaf, forumVO.message from ThreadPostVo
			// forumVO where forumVO.parentId = 0 and forumVO.postType = 'F'";
			//
			// Query query = session.createQuery(SQL_QUERY);
			//
			// for (Iterator it = query.iterate(); it.hasNext();) {
			// Object[] row = (Object[]) it.next();
			// threadPostVo.setPostId(Integer.parseInt(row[0].toString()));
			// threadPostVo.setParentId(new Integer(row[1].toString()));
			// threadPostVo.setTitle(Utility.getStringValue(row[2].toString(),""));
			// threadPostVo.setIsLeaf(Utility.getStringValue(row[3].toString(),""));
			// threadPostVo.setMessage(Utility.getStringValue(row[4].toString(),""));
			//
			// }

		} catch (Exception e) {
			logger.error("Exception=" + e.toString());

			throw new NGFException(e.getMessage());
		}
		return threadPostVo;
	}

	/**
	 * getChildNodes to to get the child nodes of the tree
	 * 
	 * 
	 * @param int,Session
	 * @return ArrayList
	 * @throws NGFException
	 */

	public ArrayList getChildNodes(int parentNode, Session session)
			throws NGFException {
		// System.out.println("in DAO - TreeDBConnection -> getChildNodes()");
		ArrayList children = new ArrayList();

		// String sqlRootQuery = "SELECT * FROM tree WHERE parentid ='"+
		// parentNode +"'";
		List postsList = null;
		try {
			logger.info("start getChildNodes");
			postsList = session.createCriteria(Posts.class).add(
					Expression.eq("parentId", new Integer(parentNode))).add(
					Expression.eq("postType", new Character('F'))).list();

			children.addAll(postsList);

			// String SQL_QUERY = "Select forumVO.postId, forumVO.parentId,
			// forumVO.title , forumVO.isLeaf, forumVO.message from ThreadPostVo
			// forumVO where forumVO.postType = 'F' AND forumVO.parentId ="
			// + parentNode;
			// Query query = session.createQuery(SQL_QUERY);
			//
			// for (Iterator it = query.iterate(); it.hasNext();) {
			// Posts threadPostVo = new Posts();
			//
			// Object[] row = (Object[]) it.next();
			// threadPostVo.setPostId(Integer.parseInt(row[0].toString()));
			// threadPostVo.setParentId(new Integer(row[1].toString()));
			// threadPostVo.setTitle(Utility.getStringValue(row[2].toString(),""));
			// threadPostVo.setIsLeaf(Utility.getStringValue(row[3].toString(),""));
			// threadPostVo.setMessage(Utility.getStringValue(row[4].toString(),""));
			//				
			// children.add(threadPostVo);
			//
			// }

		} catch (Exception e) {
			logger.error("exception=" + e.toString());
			throw new NGFException(e.getMessage());
		}

		return children;

	}

	/**
	 * getTotalCount to to get the total count of the posts
	 * 
	 * 
	 * @param Session
	 * @return int
	 * @throws NGFException
	 */
	public int getTotalCount(Session session) throws NGFException {
		int count = 0;
		// Session session=null;
		List postsList = null;
		try {

			logger.info("start getTotalCount");
			postsList = session.createCriteria(Posts.class).add(
					Expression.eq("postType", new Character('F'))).list();
			count = postsList.size();

			// SessionFactory sessionFactory = new Configuration().configure()
			// .buildSessionFactory();
			//
			// session = sessionFactory.openSession();
			//
			// String SQL_QUERY = "Select forumVO.postId from ThreadPostVo
			// forumVO Where forumVO.postType = 'F'";
			//
			// Query query = session.createQuery(SQL_QUERY);
			//
			// for (Iterator it = query.iterate(); it.hasNext();) {
			// count++;
			// it.next();
			// }

		} catch (Exception e) {
			logger.error("exception=" + e.toString());
			throw new NGFException(e.getMessage());
		}
		return count;

	}

	/**
	 * updateForumData to to update the forum data
	 * 
	 * 
	 * @param Posts,Session
	 * @return none
	 * @throws NGFException
	 */
	public void updateForumData(Posts postsVo, Session session)
			throws NGFException {

		try {
			logger.info("start updateForumData");
			session.saveOrUpdate(postsVo);

		} catch (Exception e) {
			logger.error("Exception=" + e.toString());
			throw new NGFException(e.getMessage());

		}

	}

	/**
	 * addForumData to add data in the forum
	 * 
	 * 
	 * @param Posts,Session
	 * @return Posts
	 * @throws NGFException
	 */
	public void addForumData(Posts postsVo, Session session)
			throws NGFException {

		try {
			logger.info("Start addForumdata");
			session.saveOrUpdate(postsVo);

		} catch (Exception e) {
			logger.info("Exception=" + e.toString());
			throw new NGFException(e.getMessage());

		}

	}

	/**
	 * getForumById to get the forum by using Id
	 * 
	 * 
	 * @param Posts,Session
	 * @return Posts
	 * @throws NGFException
	 */

	public Posts getForumById(Posts postsVo, Session session)
			throws NGFException {
		// System.out.println("getForumById in DAO");
		Posts posts = null;
		try {
			logger.info("start getForumByID");
			posts = new Posts();
			posts = (Posts) session.createCriteria(Posts.class).add(
					Expression.eq("postId", new Integer(postsVo.getPostId())))
					.uniqueResult();

		} catch (Exception e) {
			logger.error("Exception=" + e.toString());
			throw new NGFException(e.getMessage());

		}
		return posts;
	}

	/**
	 * getForumPostData to get forum posts
	 * 
	 * 
	 * @param int,Session
	 * @return Posts
	 * @throws NGFException
	 */
	public Posts getForumPostData(int memberId, Session session) {

		Posts postsVo = new Posts();

		try {
			logger.info("start getForumPostData");

			postsVo = (Posts) session.createCriteria(Posts.class).add(
					Expression.eq("memberId", new Integer(memberId)))
					.uniqueResult();
			return postsVo;

		} catch (Exception e) {
			logger.error("Exception=" + e.toString());

		} finally {
		}

		return postsVo;
	}

}
